<template>
  <div class="clothes-management-container">
    <el-card>
      <div slot="header">
        <span>衣物类型管理</span>
        <el-button style="float: right; padding: 3px 0" type="text" @click="handleAdd">新增衣物类型</el-button>
      </div>
      
      <el-table
        v-loading="loading"
        :data="clothesTypes"
        style="width: 100%">
        <el-table-column
          prop="id"
          label="ID"
          width="80">
        </el-table-column>
        <el-table-column
          prop="name"
          label="衣物名称"
          width="180">
        </el-table-column>
        <el-table-column
          prop="description"
          label="描述">
        </el-table-column>
        <el-table-column
          label="创建时间"
          width="180">
          <template slot-scope="scope">
            {{ formatDate(scope.row.createTime) }}
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          width="150">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    
    <!-- 新增/编辑对话框 -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="500px">
      <el-form :model="clothesForm" :rules="clothesRules" ref="clothesForm" label-width="100px">
        <el-form-item label="衣物名称" prop="name">
          <el-input v-model="clothesForm.name"></el-input>
        </el-form-item>
        <el-form-item label="描述" prop="description">
          <el-input type="textarea" v-model="clothesForm.description" rows="3"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitClothes">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { getClothesTypes, createClothesType, updateClothesType, deleteClothesType } from '@/api/clothesType'
import { formatDate } from '@/utils/date'

export default {
  name: 'ClothesManagement',
  data() {
    return {
      loading: false,
      clothesTypes: [],
      dialogVisible: false,
      dialogTitle: '新增衣物类型',
      clothesForm: {
        id: null,
        name: '',
        description: ''
      },
      clothesRules: {
        name: [
          { required: true, message: '请输入衣物名称', trigger: 'blur' },
          { max: 50, message: '名称长度不能超过50个字符', trigger: 'blur' }
        ],
        description: [
          { max: 200, message: '描述长度不能超过200个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.fetchClothesTypes()
  },
  methods: {
    formatDate,
    async fetchClothesTypes() {
      this.loading = true
      try {
        const res = await getClothesTypes()
        if (res.code === 200) {
          this.clothesTypes = res.data
        } else {
          this.$message.error(res.message)
        }
      } catch (error) {
        this.$message.error('获取衣物类型列表失败')
      } finally {
        this.loading = false
      }
    },
    handleAdd() {
      this.dialogTitle = '新增衣物类型'
      this.clothesForm = {
        id: null,
        name: '',
        description: ''
      }
      this.dialogVisible = true
    },
    handleEdit(row) {
      this.dialogTitle = '编辑衣物类型'
      this.clothesForm = {
        id: row.id,
        name: row.name,
        description: row.description
      }
      this.dialogVisible = true
    },
    handleDelete(row) {
      this.$confirm('确认删除该衣物类型?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async () => {
        try {
          const res = await deleteClothesType(row.id)
          if (res.code === 200) {
            this.$message.success('删除成功')
            this.fetchClothesTypes()
          } else {
            this.$message.error(res.message)
          }
        } catch (error) {
          this.$message.error('删除衣物类型失败')
        }
      }).catch(() => {})
    },
    submitClothes() {
      this.$refs.clothesForm.validate(async valid => {
        if (valid) {
          try {
            let res
            if (this.clothesForm.id) {
              res = await updateClothesType(this.clothesForm)
            } else {
              res = await createClothesType(this.clothesForm)
            }
            
            if (res.code === 200) {
              this.$message.success(this.clothesForm.id ? '更新成功' : '新增成功')
              this.dialogVisible = false
              this.fetchClothesTypes()
            } else {
              this.$message.error(res.message)
            }
          } catch (error) {
            this.$message.error(this.clothesForm.id ? '更新衣物类型失败' : '新增衣物类型失败')
          }
        }
      })
    }
  }
}
</script>

<style scoped>
.clothes-management-container {
  padding: 20px;
}
</style> 